package com.example.qxfw.csnl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.qxfw.common.entity.WaterloggingYzdisppara;
import com.example.qxfw.common.entity.zy.WaterLoggingLevelSetting;
import com.example.qxfw.common.mapper.WaterloggingYzdispparaMapperXml;
import com.example.qxfw.csnl.mapper.WaterLoggingLevelSettingMapper;
import com.example.qxfw.csnl.service.YzszService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class YzszServiceImpl implements YzszService {
    @Autowired
    private WaterLoggingLevelSettingMapper waterLoggingLevelSettingMapper;

    @Autowired
    private WaterloggingYzdispparaMapperXml waterloggingYzdispparaMapperXml;

    @Override
    public WaterLoggingLevelSetting insertOfWaterLoggingLevelSetting(WaterLoggingLevelSetting waterLoggingLevelSetting) {
        List<WaterLoggingLevelSetting> list = waterLoggingLevelSettingMapper.selectList(new LambdaQueryWrapper<WaterLoggingLevelSetting>()
                .select(WaterLoggingLevelSetting::getId));
        int[] ints = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ints[i] = list.get(i).getId();
        }
        int i = Arrays.stream(ints).max().getAsInt();
        WaterLoggingLevelSetting waterLoggingLevelSetting1 = new WaterLoggingLevelSetting();
        waterLoggingLevelSetting1.setId(i+1);
        waterLoggingLevelSetting1.setColor(waterLoggingLevelSetting.getColor());
        waterLoggingLevelSetting1.setWatergrade(waterLoggingLevelSetting.getWatergrade());
        waterLoggingLevelSetting1.setGradetype(waterLoggingLevelSetting.getGradetype());
        waterLoggingLevelSetting1.setRainvalue(waterLoggingLevelSetting.getRainvalue());
        int insert = waterLoggingLevelSettingMapper.insert(waterLoggingLevelSetting1);
        if (insert > 0) {
            return waterLoggingLevelSetting1;
        }else {
            return null;
        }
    }

    @Override
    public int deleteById(int id) {
        return waterLoggingLevelSettingMapper.deleteById(id);
    }

    @Override
    public WaterLoggingLevelSetting updateById(WaterLoggingLevelSetting waterLoggingLevelSetting) {
        int update = waterLoggingLevelSettingMapper.update(waterLoggingLevelSetting, new UpdateWrapper<WaterLoggingLevelSetting>()
                .eq("id", waterLoggingLevelSetting.getId()));
        if (update > 0) {
            return waterLoggingLevelSetting;
        }else {
            return null;
        }
    }

    @Override
    public List<WaterloggingYzdisppara> getAllWaterLoggingLevelSetting() {
        return waterloggingYzdispparaMapperXml.selectAll();
    }
}
